<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateDoubaoApiLogTable extends Migrator
{
    /**
     * 创建豆包AI API日志表
     */
    public function change()
    {
        // 创建豆包AI API日志表
        $table = $this->table('doubao_api_log', [
            'id' => false,
            'primary_key' => ['id'],
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_unicode_ci',
            'comment' => '豆包AI API调用日志表'
        ]);
        
        $table->addColumn('id', 'integer', [
            'identity' => true,
            'signed' => false,
            'comment' => '主键ID'
        ])
        ->addColumn('api_url', 'string', [
            'limit' => 255,
            'comment' => 'API地址'
        ])
        ->addColumn('model', 'string', [
            'limit' => 100,
            'comment' => '使用的模型'
        ])
        ->addColumn('request_data', 'text', [
            'comment' => '请求数据(JSON格式)'
        ])
        ->addColumn('response_data', 'text', [
            'comment' => '响应数据(JSON格式)'
        ])
        ->addColumn('user_id', 'integer', [
            'signed' => false,
            'default' => 0,
            'comment' => '用户ID'
        ])
        ->addColumn('ip_address', 'string', [
            'limit' => 45,
            'comment' => 'IP地址'
        ])
        ->addColumn('user_agent', 'string', [
            'limit' => 500,
            'null' => true,
            'comment' => '用户代理'
        ])
        ->addColumn('request_id', 'string', [
            'limit' => 100,
            'null' => true,
            'comment' => '请求ID'
        ])
        ->addColumn('tokens_used', 'integer', [
            'signed' => false,
            'default' => 0,
            'comment' => '使用的token数量'
        ])
        ->addColumn('processing_time', 'decimal', [
            'precision' => 10,
            'scale' => 4,
            'default' => 0.0000,
            'comment' => '处理时间(秒)'
        ])
        ->addColumn('status', 'string', [
            'limit' => 20,
            'default' => 'success',
            'comment' => '状态(success/error)'
        ])
        ->addColumn('error_message', 'text', [
            'null' => true,
            'comment' => '错误信息'
        ])
        ->addColumn('create_time', 'integer', [
            'signed' => false,
            'comment' => '创建时间'
        ])
        ->addColumn('update_time', 'integer', [
            'signed' => false,
            'null' => true,
            'comment' => '更新时间'
        ])
        ->addIndex(['user_id'], ['name' => 'idx_user_id'])
        ->addIndex(['create_time'], ['name' => 'idx_create_time'])
        ->addIndex(['model'], ['name' => 'idx_model'])
        ->addIndex(['status'], ['name' => 'idx_status'])
        ->create();
    }
}